//
//  InfoTableViewCell.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/18.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "InfoTableViewCell.h"
#import <UIImageView+WebCache.h>

@interface InfoTableViewCell ()
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UIImageView *contentImg;
@property (nonatomic,strong) UILabel *detailLb;
@end
@implementation InfoTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self configUI];
    }
    return self;
}
- (void)configUI{
    UIView *contentView = self.contentView;
    [contentView addSubview:self.titleLb];
    [contentView addSubview:self.contentImg];
    [contentView addSubview:self.detailLb];
}
- (void)setModel:(InfoModel *)model{
    _model = model;
//    暂时隐藏
    _titleLb.attributedText = [self detailH5Str:_model.title];
    _titleLb.alpha = 0;
    
    _detailLb.attributedText = [self detailH5Str:_model.content];
//    _model.img = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513595661098&di=d97347a855cc1b15f55036651a9e7b9c&imgtype=0&src=http%3A%2F%2Fs1.sinaimg.cn%2Fmw690%2F005BHolfzy6Loubbf7a10";
    if (_model.img.length>2) {
        [_contentImg sd_setImageWithURL:[NSURL URLWithString:_model.img]];
        _contentImg.hidden = NO;
    }
    [self layoutSubviews];
}
- (NSMutableAttributedString *)detailH5Str:(NSString *)text{
    NSAttributedString *briefAttrStr = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithAttributedString:briefAttrStr];
 
    return attr;
}
- (void)layoutSubviews{
    CGFloat gap = 15;
    CGSize titleSize = [_detailLb.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(KWidth - 2*gap, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(gap));
        make.right.equalTo(self).offset(-gap);
        make.top.equalTo(@(gap));
        make.height.equalTo(@13);
    }];
    CGFloat height = !_contentImg.hidden?:0.001;
  
    [_contentImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200, 200));
        make.centerX.equalTo(self);
        make.top.equalTo(_titleLb.mas_bottom).offset(gap);
//        make.height.equalTo(@(height));
    }];
    [_detailLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLb);
        make.right.equalTo(_titleLb);
        if (_contentImg.hidden) {
               make.top.equalTo(_titleLb.mas_bottom).offset(gap);
        }else{
               make.top.equalTo(_titleLb.mas_bottom).offset(230);
        }
   
        make.height.equalTo(@(titleSize.height +10));
    }];
 
}
- (UILabel *)titleLb{
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.textColor = RGBCOLOR(187, 187, 187);
    }
    return _titleLb;
}
- (UIImageView *)contentImg{
    if (!_contentImg) {
        _contentImg = [UIImageView new];
        _contentImg.hidden = YES;
        _contentImg.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _contentImg;
}
- (UILabel *)detailLb{
    if (!_detailLb) {
        _detailLb = [UILabel new];
        _detailLb.numberOfLines = 0;
    }
    return _detailLb;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];


}

@end
